import win32gui,win32con,win32api
import time
def show_handles():
    hd = win32gui.GetDesktopWindow()
    # 获取所有子窗口
    hwndChildList = []
    win32gui.EnumChildWindows(hd, lambda hwnd, param: param.append(hwnd), hwndChildList)
    for hwnd in hwndChildList:
        print("句柄：",hwnd," 标题： ",str(win32gui.GetWindowText(hwnd)))


def get_RX1_INF():
    paper_capacity=-1
    remaining_paper_num=-1
    printer_status=" "
    hwnd_whole = win32gui.FindWindow(None, "RX1 Information")
    #print("打印机窗口句柄为：" + str(hwnd_whole))
    if hwnd_whole != 0:
        button_handle = win32gui.FindWindowEx(hwnd_whole, 0, 0, "更新")
        win32gui.ShowWindow(hwnd_whole, win32con.SW_SHOW)  # 显示激活窗口
        win32gui.ShowWindow(hwnd_whole, win32con.SW_SHOWMINIMIZED)  # 最小化显示窗口
        #print("更新控件句柄为:" + str(button_handle))
        win32api.SendMessage(button_handle, win32con.BM_CLICK, 0, 0)
        win32gui.ShowWindow(hwnd_whole, win32con.SW_HIDE)  # 隐藏窗口
        #print("已点击更新")
    # GetDesktopWindow 获得代表整个屏幕的一个窗口（桌面窗口）句柄
    hd = win32gui.GetDesktopWindow()
    # 获取所有子窗口
    hwndChildList = []
    win32gui.EnumChildWindows(hd, lambda hwnd, param: param.append(hwnd), hwndChildList)
    for i in range(len(hwndChildList)):
        if win32gui.GetWindowText(hwndChildList[i]) == "介质信息":
            paper_capacity=int(win32gui.GetWindowText(hwndChildList[i + 1]))
            remaining_paper_num=int(win32gui.GetWindowText(hwndChildList[i + 2]))
        elif win32gui.GetWindowText(hwndChildList[i]) == "打印机状态":

            buffer_size = win32gui.SendMessage(hwndChildList[i+2], win32con.WM_GETTEXTLENGTH, 0, 0)+1
            buffer = win32gui.PyMakeBuffer(buffer_size)
            win32gui.SendMessage(hwndChildList[i+2], win32con.WM_GETTEXT, buffer_size, buffer)
            address, length = win32gui.PyGetBufferAddressAndLen(buffer[:-1])
            printer_status = win32gui.PyGetString(address, length)
    return paper_capacity,remaining_paper_num,printer_status



if __name__ == "__main__":
    #show_handles()
    a,b,c=get_RX1_INF()
    print(a,b,c)
